#include "FrequencyTableRestorer.h"
#include "FrequencyTable.h"
#include "HuffmanOptions.h"
#include "DataSource.h"

#ifdef _DEBUG
#include <iostream>
#endif

FrequencyTable* FrequencyTableRestorer::buildTable(DataSource* s)
{
	size_t len = 0;
	for (size_t i = 0;  i < headSizeBytes; ++i)
	{
		uint8_t byte = s->getNextByte();
		len |= byte << ((headSizeBytes - i - 1) * 8);
	}	
#ifdef _DEBUG
	std::cout << "Frequency table size is " << len << std::endl;
#endif
	uint8_t* buf = new uint8_t[len];
	for (size_t i = 0; i < len; ++i)
	{
		buf[i] = s->getNextByte();
	}
	FrequencyTable* table = new FrequencyTable(buf, len);
	delete[] buf;
	return table;
}